package com.bluexmicro.ota2.ui.screen.asset

import android.annotation.SuppressLint
import android.text.TextUtils
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import com.bluexmicro.ota2.picker.device.LaunchDevicePicker
import com.bluexmicro.ota2.picker.file.LaunchFilePicker
import com.bluexmicro.ota2.ui.custom.DisplayItem
import com.bluexmicro.ota2.ui.custom.ErrorDialog
import com.bluexmicro.ota2.ui.custom.KeepScreenOnView
import com.bluexmicro.ota2.ui.custom.SubTopBar
import com.bluexmicro.ota2.ui.custom.TimeConsumingDialog
import com.bluexmicro.ota2.ui.custom.formatBytes
import com.bluexmicro.ota2.ui.custom.stepContent
import com.bluexmicro.ota2.ui.custom.stepTitle

sealed class AssetType(val name: String, val ext: String) {
    object Face : AssetType("下载表盘", "bxface")
    object Dict : AssetType("下载词典", "bxdict")
}


@SuppressLint("MissingPermission")
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AssetScreen(navHostController: NavHostController, type: AssetType) {
    val viewModel: AssetViewModel = viewModel()
    val context = LocalContext.current

    val item = remember {
        viewModel.item
    }

    val target = remember {
        viewModel.target
    }

    val canUpdate = remember {
        viewModel.canUpdate
    }

    val progress = remember {
        derivedStateOf {
            (viewModel.progress.value * 100).toInt()
        }
    }

    val updating = remember {
        viewModel.updating
    }

    var error by remember {
        viewModel.error
    }

    var timeConsuming by remember {
        viewModel.timeConsuming
    }


    val filePicker = rememberLauncherForActivityResult(contract = LaunchFilePicker(), onResult = {
        if (it != null) {
            viewModel.onFileSelected(it)
        }
    })

    val devicePicker = rememberLauncherForActivityResult(
        contract = LaunchDevicePicker(),
        onResult = {
            if (it != null) {
                viewModel.onDeviceSelected(it)
            }
        }
    )

    Scaffold(
        topBar = {
            SubTopBar(title = type.name) {
                navHostController.popBackStack()
            }
        }
    ) {
        LazyColumn(
            modifier = Modifier.padding(it),
            contentPadding = PaddingValues(16.dp),
        ) {

            stepTitle(1, "选择资源包（*.${type.ext}）", item.value != null)

            stepContent {


                item.value?.let {
                    Card {
                        DisplayItem(it.name, "Size: ${formatBytes(it.rawData.size.toLong())}")
                    }
                    Spacer(modifier = Modifier.height(8.dp))
                }

                Button(onClick = { filePicker.launch(type.ext) }, enabled = !updating.value) {
                    Text(text = "选择")
                }
            }

            stepTitle(2, "选择目标设备", target.value != null)

            stepContent {

                target.value?.let {
                    Card {
                        val name = if (TextUtils.isEmpty(it.device.name)) {
                            it.scanRecord?.deviceName ?: "N/A"
                        } else {
                            it.device.name
                        }
                        DisplayItem(title = name, desc = target.value?.device?.address ?: "未选择")
                    }
                    Spacer(modifier = Modifier.height(16.dp))
                }

                Button(onClick = { devicePicker.launch(Unit) }, enabled = !updating.value) {
                    Text(text = "选择")
                }
            }

            val desc = if (updating.value) {
                if (progress.value == 0) {
                    ": 正在连接、初始化设备..."
                } else {
                    ": ${progress.value}%"
                }
            } else {
                ""
            }
            stepTitle(3, "下载$desc", canUpdate.value)

            stepContent {
                Button(onClick = { viewModel.startUpdate(context) }, enabled = canUpdate.value) {
                    Text(text = "开始下载")
                }
                KeepScreenOnView(keepOn = updating.value, modifier = Modifier.size(8.dp))
            }
        }
    }

    if (error != null) {
        ErrorDialog(error = error!!) {
            error = null
        }
    } else {
        if (timeConsuming != null) {
            TimeConsumingDialog(timeConsuming = timeConsuming!!) {
                timeConsuming = null
            }
        }
    }
}